// pages/contentAdd/contentAdd.js

import {refundApplication} from "../../services/refundReq";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsName: '',
        reason: '',
        goodOrderNumber: null,
        timer:null,
        count:60,
        content:"立即申请退款"


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(`options.order-->`, options);
        this.setData(
            {
                goodsName: options.goodsName,
                goodOrderNumber: options.goodOrderNumber
            }
        )


    },
    error(){
        clearInterval(this.data.timer);
        this.setData({
            timer:null,
            count:60,
            content:"立即申请退款"
        })

    },
    dboll(){
        const self = this;
        if(self.data.timer) return ;
        const timer = setInterval(()=>{
            if(self.data.count>0){
                self.setData({
                    count:self.data.count-1,
                    content:`请至少等待${self.data.count}s`,
                    timer
                })
            }else{
                self.error();
            }

        },1000)

        
    },


    // 发布活动风采
    handleRefundNow() {
        const form = this.data;
        if (!form.reason) return wx.showToast({
            title: '请输入理由',
            icon: 'error',
        })

        const params = {
            orderNumber: form.goodOrderNumber,
            refundReason: form.reason
        }
        // console.log(`params-->`,    params)
        wx.showLoading();
        this.dboll();

        if(this.data.count !== 60) {
            return wx.showToast({
              title: '请不要重复点击',
              icon:"error"
            })
        }

        refundApplication(params, res => {
            let type = 'success';
            if (res.data.code !== 0) {
                this.error();
                type = "error";};
            
            return wx.showToast({
                title: res.data.msg,
                icon: type
            })
        })
    },

    handleCommentChange(e) {
        this.setData({
            reason: e.detail.value
        })
    },


})
